// Copyright (c) 2010, Anthony Cassidy
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are 
// permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list 
//         of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this 
//         list of conditions and the following disclaimer in the documentation and/or 
//         other materials provided with the distribution.
//     * Neither the name of the AntsAdventureGameCreator nor the names of its contributors 
//         may be used to endorse or promote products derived from this software without 
//         specific prior written permission.
// 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
#include "XStdafx.h"
#include "XDialogForLayout.h"

XDialogForLayout::XDialogForLayout(QVector<QString> verbs, int pixelsHeight, QFont font)
{
    m_rowCount = (int)floor(sqrt(verbs.size()*1.0));
    m_font = font;
    m_columnCount = verbs.size()==0? 1 : (int)ceil(verbs.size()/floor(sqrt(verbs.size()*1.0)));
    m_totalHeight = pixelsHeight;
    m_recommendedFontSizeInPoints = 8;
    m_verbs = verbs;
    ASSERT(m_totalHeight>1 && "THIS NEEDS TO BE BIGGGER THAN ONE");

}
QRect XDialogForLayout::GetSmallestRectangleContainingAllVerbs(QFont font)
{



    QFontMetrics metrics(font);

    int maxWidth = metrics.height();
    int maxHeight= metrics.height();
    for( int i = 0;i<m_verbs.size();i++)
    {
        QString text = m_verbs.at(i);
        QRect rect = metrics.boundingRect(text);
        if(rect.width()>maxWidth)
            maxWidth = rect.width();
        if(rect.height()>maxHeight)
            maxHeight = rect.height();
    }
    int totalMaxWidth = maxWidth*(m_columnCount==0? 1:m_columnCount);
    int totalMaxHeight = maxHeight*(m_rowCount==0? 1:m_rowCount);
    QRect rect(0,0,totalMaxWidth, totalMaxHeight);

    Log::Sizing(QString("%1 %2 %3 %4").arg(rect.left()).arg( rect.right()).arg( rect.top()).arg(rect.bottom()));
    return rect;

}


void XDialogForLayout::PopulateRectList()
{

    QRect smallestVerbRect = GetSmallestRectangleContainingAllVerbs(m_font);

    double inflator = m_totalHeight/smallestVerbRect.height();

    m_rects.clear();
    for(int i = 0;i<(m_columnCount * m_rowCount);i++)
    {
        int maxHeight = smallestVerbRect.height()/m_rowCount;
        int maxWidth = smallestVerbRect.width()/m_columnCount;

        int row = i/m_columnCount;
        int col = i%m_columnCount;

        QPointF topLeft = QPoint(col*maxWidth,row*maxHeight)*inflator;
        QPointF bottomRight = (QPoint((col+1)*maxWidth,(row+1)*maxHeight)+QPoint(1,1)) *inflator;

        QRect rect2(topLeft.toPoint(),bottomRight.toPoint()- QPoint(1,1));
        //Log::Images(" (%d, %d) - (%d, %d)", rect.top(),rect.left(), rect.bottom(), rect.right());
        m_rects.push_back(rect2);
    }


    int pixelSize = m_font.pointSize();
    m_recommendedFontSizeInPoints = pixelSize * inflator;
}


double XDialogForLayout::GetRecommendedFontSizeInPoints()
{
    return m_recommendedFontSizeInPoints;
}

XDialogForLayout::~XDialogForLayout()
{

}
